﻿using AutoMapper;
using Common.Enum;
using Entity.ERP.DTO.SaleDeliveryOrder;
using Entity.ERP.Entity;
using Entity.ERP.View.SaleDeliveryOrder;
using Entity.ERP.View.SaleDeliveryOrderItem;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.ERP
{
    public class SaleDeliveryService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public SaleDeliveryService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取销售发货单列表
         */
        public async Task<RequestResponseModel<PageResult<SaleDeliveryOrderView>>> GetSaleDeliveryList(SaleDeliverySearchDTO saleDeliveryOrderSearchDTO)
        {
            //筛选
            var query = _context.SaleDeliveryOrderView
                .WhereIF(a => a.Code.Contains(saleDeliveryOrderSearchDTO.Code), !string.IsNullOrEmpty(saleDeliveryOrderSearchDTO.Code))
                .WhereIF(a => a.CardNo.Contains(saleDeliveryOrderSearchDTO.CardNo), !string.IsNullOrEmpty(saleDeliveryOrderSearchDTO.CardNo))
                .WhereIF(a => a.Contact.Contains(saleDeliveryOrderSearchDTO.CustomerName), !string.IsNullOrEmpty(saleDeliveryOrderSearchDTO.CustomerName))
                .WhereIF(a => a.Status == saleDeliveryOrderSearchDTO.Status, saleDeliveryOrderSearchDTO.Status != null)
                .WhereIF(a => a.ExamStatus == saleDeliveryOrderSearchDTO.ExamStatus, saleDeliveryOrderSearchDTO.ExamStatus != null)
                ;

            //返回列表
            var saleDeliveryOrderList = await query
                .Page(saleDeliveryOrderSearchDTO.CurrentPage, saleDeliveryOrderSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<SaleDeliveryOrderView>(saleDeliveryOrderList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 销售发货单子单列表
         */
        public async Task<RequestResponseModel<PageResult<SaleDeliveryOrderItemView>>> GetSaleDeliveryOrderItemList(SaleDeliveryItemSearchDTO saleDeliveryItemSearchDTO)
        {
            //筛选
            var query = _context.SaleDeliveryOrderItemView
                .Where(a => a.SaleDeliveryOrderId == saleDeliveryItemSearchDTO.Id)
                ;

            //返回列表
            var saleDeliveryOrderItemList = await query
                .Page(saleDeliveryItemSearchDTO.CurrentPage, saleDeliveryItemSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<SaleDeliveryOrderItemView>(saleDeliveryOrderItemList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增销售发货单
         */
        public async Task<RequestResponseModel<string>> AddSaleDeliveryOrder(SaleDeliveryAddDTO saleDeliveryAddDTO)
        {
            //处理主单单据
            var saleDeliveryOrder = _mapper.Map<SaleDeliveryModel>(saleDeliveryAddDTO);
            saleDeliveryOrder.Id = Guid.NewGuid().ToString();
            saleDeliveryOrder.Status = StateEnum.SaleType.待推送;
            saleDeliveryOrder.ExamStatus = StateEnum.ExamStatus.待审核;
            saleDeliveryOrder.CreateTime = DateTime.Now;

            //处理子单数据
            var saleDeliveryOrderItemList = _mapper.Map<List<SaleDeliveryItemModel>>(saleDeliveryAddDTO.items);
            foreach (var item in saleDeliveryOrderItemList)
            {
                item.Id = Guid.NewGuid().ToString();
                item.SaleDeliveryOrderId = saleDeliveryOrder.Id;
                item.CardNo = saleDeliveryOrder.CardNo;
            }

            //修改并保存数据库
            _context.SaleDeliveryOrder.Add(saleDeliveryOrder);
            _context.SaleDeliveryOrderItem.AddRange(saleDeliveryOrderItemList);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", saleDeliveryOrder.Id);
        }

        /**
         * 编辑销售发货单
         */
        public async Task<RequestResponseModel<string>> PutSaleDeliveryOrder(SaleDeliveryUpdateDTO saleDeliveryUpdateDTO)
        {
            //校验id有效性
            var query = await Valid.ValidId(_context.SaleDeliveryOrder, saleDeliveryUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "销售订单id无效", saleDeliveryUpdateDTO.Id);
            }

            //只有待审核的单据才能编辑
            if (query.ExamStatus != StateEnum.ExamStatus.待审核)
            {
                return RequestResponse.CreateResponse(400, "只有待审核的订单才能编辑", saleDeliveryUpdateDTO.Id);
            }

            //处理主单
            var saleDeliveryOrder = _mapper.Map<SaleDeliveryModel>(saleDeliveryUpdateDTO);
            //处理子单
            var saleDeliveryOrderItemList = _mapper.Map<List<SaleDeliveryItemModel>>(saleDeliveryUpdateDTO.Items);

            //修改并保存数据库
            _context.SaleDeliveryOrder.Update(saleDeliveryOrder);
            _context.SaleDeliveryOrderItem.UpdateRange(saleDeliveryOrderItemList);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", query.Id);
        }

        /**
         * 删除销售订单
         */
        public async Task<RequestResponseModel<string>> DeleteSaleDeliveryOrder(string Id)
        {
            //校验id有效性
            var query = await Valid.ValidId(_context.SaleDeliveryOrder, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "id无效", Id);
            }

            //已审核的单据无法删除
            if (query.Status != StateEnum.SaleType.待推送)
            {
                return RequestResponse.CreateResponse(400, "只有待生成的单据才能删除", Id);
            }

            //查找该id下的子单
            var saleDeliveryOrderItemList = _context.SaleDeliveryOrderItem
                .Where(a => a.Id.Equals(Id));

            //操作数据库并保存
            _context.SaleDeliveryOrder.Remove(query);
            _context.SaleDeliveryOrderItem.RemoveRange(saleDeliveryOrderItemList);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 销售发货单 - 审核通过
         */
        public async Task<RequestResponseModel<string>> PassSaleDeliveryOrder(string Id)
        {
            //校验id
            var query = await Valid.ValidId(_context.SaleDeliveryOrder, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //校验状态
            if (query.ExamStatus != StateEnum.ExamStatus.待审核)
            {
                return RequestResponse.CreateResponse(400, "只有待审核的单据才可以审核通过", Id);
            }

            //修改数据
            query.ExamStatus = StateEnum.ExamStatus.审核通过;

            //修改并保存数据库
            _context.SaleDeliveryOrder.Update(query);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "审核通过成功", Id);
        }

        /**
         * 销售发货单 - 审核不通过
         */
        public async Task<RequestResponseModel<string>> UnPassSaleDeliveryOrder(string Id)
        {
            //校验id
            var query = await Valid.ValidId(_context.SaleDeliveryOrder, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //校验状态
            if (query.ExamStatus != StateEnum.ExamStatus.待审核)
            {
                return RequestResponse.CreateResponse(400, "只有待审核的单据才可以审核不通过", Id);
            }

            //修改数据
            query.ExamStatus = StateEnum.ExamStatus.审核不通过;

            //修改并保存数据库
            _context.SaleDeliveryOrder.Update(query);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "审核不通过成功", Id);
        }

        /**
         * 销售发货单 - 反审核
         */
        public async Task<RequestResponseModel<string>> CancelPassSaleDeliveryOrder(string Id)
        {
            //校验id
            var query = await Valid.ValidId(_context.SaleDeliveryOrder, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //校验状态
            if (query.Status != StateEnum.SaleType.待推送)
            {
                return RequestResponse.CreateResponse(400, "只有待推送的单据才可以反审核", Id);
            }

            //修改数据
            query.ExamStatus = StateEnum.ExamStatus.待审核;

            //修改并保存数据库
            _context.SaleDeliveryOrder.Update(query);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "反审核成功", Id);
        }

        /**
         * 推送发货
         */
    }
}
